/** @(#)
 *
 * Project:  schtech
 *
 * Modify Information:
 * =============================================================================
 *   Author           Date           Description
 *   ------------     ----------     --------------------------------------------
 *   name             2014年2月26日        description
 *
 *
 * Copyright Notice:
 * =============================================================================
 *       Copyright 2014 Schtech Software, Inc. All rights reserved.
 *
 *       This software is the confidential and proprietary information of
 *       Shenzhen SCHTECH Software Co., Ltd. ("Confidential Information").
 *       You shall not disclose such Confidential Information and shall use it
 *       only in accordance with the terms of the license agreement you entered
 *       into with Schtech.
 *
 * Warning:
 * =============================================================================
 *
 */
package net.schtech.source.base.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.kit.StringKit;


/** 
 * <p><strong>Description:</strong> 过滤URL，如以.sch结尾或例外的配置，则允许通过</p>
 * @Company 深圳盛创汇科技有限公司
 * @author  <a href="mailto: duanxuhua@163.com">段绪华</a>
 * @version 1 
 * @Copyright Copyright 2014, Shenzhen Schtech Co., Ltd.
 * All right reserved.
 */
public class UrlFilter implements Filter {
    
    private final static Logger LOGGER = LoggerFactory.getLogger(UrlFilter.class);
    
    protected String endWith = ".sch";
    protected String []ignores = new String[]{"error.jsp","404.jsp","login.jsp"};
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        String url = request.getRequestURL().toString().toLowerCase();
        if(LOGGER.isDebugEnabled()){
            LOGGER.debug("request url: " + url);
        }
        boolean isIgnore = false;
        if(!url.endsWith(endWith)){
            for(String ignore: ignores){
                if(url.endsWith(ignore)){
                    isIgnore = true;
                    break;
                }
            }
            if(isIgnore){
                if(LOGGER.isDebugEnabled()){
                    LOGGER.debug("request url is valid");
                }
                chain.doFilter(req, resp);
            }else{
                if(LOGGER.isDebugEnabled()){
                    LOGGER.debug("request url is inValid");
                }
                response.sendRedirect(request.getContextPath()+"/app/admin/public/error.jsp");
            }
        }else{
            chain.doFilter(req, resp);
        }
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        String endWithStr = filterConfig.getInitParameter("endWith");
        if(StringKit.notBlank(endWithStr)){
            this.endWith = endWithStr.toLowerCase();
        }
        String ignoresStr = filterConfig.getInitParameter("ignore");
        if(StringKit.notBlank(ignoresStr)){
            this.ignores = ignoresStr.split(",");
        }
        if(LOGGER.isDebugEnabled()){
            LOGGER.debug("init UrlFilter parameter endWith:" + endWithStr);
            LOGGER.debug("init UrlFilter parameter ignores:[" + ignoresStr + "]");
        }
    }

}
